package com.zadsdk;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Debug;
import android.os.Process;
import android.util.Log;

import java.util.List;

public class RqSdk {

    private static boolean isAttachBaseContext = false;

    public static void attachBaseContext(Context base) {
        isAttachBaseContext = true;
    }

    public static void applicationOnCreate(Application application) {
        if (!isAttachBaseContext) {
            LogEx.e("attachBaseContext not called");
            return;
        }
        // 广告sdk，只在主进程中调用
        if (application.getPackageName().equals(getProcessName(application))) {
            try {
                ApplicationInfo appInfo = application.getPackageManager().getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA);
                String channel = getMeiTuSubChannel(application, appInfo.metaData.getString("mychannel"));
                LogEx.d("zadsdk channel=" + channel);
                ZADSDK.create(application, channel);
            } catch (Throwable t) {
            }
        }
    }

    public static void start(Context context) {
    }

    public static void resume(Context context) {
        start(context);
    }

    public static void impressionLog(String slotID, String creative_ID, String landingPage) {
        runtimeLog("impressionLog", slotID, creative_ID, landingPage);
    }

    public static void clickLog(String slotID, String creative_ID, String landingPage) {
        runtimeLog("clickLog", slotID, creative_ID, landingPage);
    }

    private static void runtimeLog(String action, String slotID, String creative_ID, String landingPage) {
        try {
            Log.d("parbatSDK","" + action + "," + slotID + "," + creative_ID + "," + landingPage);
//            Class<?> cls = DexUtil.getPathClassLoader().loadClass("com.rqsdk.dex.runtimelog.RuntimeLogManager");
//            Method method = cls.getMethod("getInstance");
//            Object instance = method.invoke(null);
//            method = instance.getClass().getMethod("addRealTimeLogParbatAd", String.class, String.class, String.class, String.class);
//            method.invoke(instance, action, slotID, creative_ID, landingPage);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
        if (infos == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == Process.myPid()) {
                return info.processName;
            }
        }
        return null;
    }

    private static String getMeiTuSubChannel(Context context, String mainChannel) {
        if (mainChannel.startsWith("D1013_MT") || mainChannel.startsWith("AD_Z100")) {
            if (context.getPackageName().equals("com.mt.mtxx.mtxx")) {
                return "AD_Z1003";
            } else if (context.getPackageName().equals("com.meitu.meiyancamera")) {
                return "AD_Z1002";
            } else if (context.getPackageName().equals("com.meitu.wheecam")) {
                return "D1013_MT_CZP";
            } else if (context.getPackageName().equals("com.meitu.makeup")) {
                return "D1013_MT_MZXJ";
            } else if (context.getPackageName().equals("com.meitu.poster")) {
                return "AD_Z1001";
            }
        }
        return mainChannel;
    }
}
